<?php

namespace app\model;

use think\Model;
use think\facade\Request;

class MemberBuyer extends Model
{
    protected $pk = 'userid';

    public function getAmountAttr($value)
    {
        return sprintf("%.2f", $value);
    }
    public function getCoinAttr($value)
    {
        return sprintf("%.2f", $value);
    }
    public function getIdcardimgurlAttr($value)
    {
        return \tool\Url::FileConversionUrl($value);
    }
    public function getIdcardimgurl2Attr($value)
    {
        return \tool\Url::FileConversionUrl($value);
    }
    public function getAvatarAttr($value)
    {
        return \tool\Url::FileConversionUrl($value);
    }
    public function getIdverifystatusAttr($value)
    {
        $status = [0 => '未绑定', 1 => '审核中', 2 => '已绑定', 3 => '审核失败'];
        return $status[$value];
    }

    public function info()
    {
        return $this->hasOne('memberBuyerInfo', 'userid', 'userid');
    }

    public function platform()
    {
        return $this->hasMany('MemberBuyerPlatformInfo', 'userid', 'userid');
    }

    public function withdraw()
    {
        return $this->hasMany('Withdraw', 'userid', 'userid');
    }

    public function setPasswordAttr($value)
    {
        return $this->convertedPssword($value);
    }

    public static function useridGetUserInfo($userid)
    {
        return self::where('userid', $userid)->find();
    }

    public static function phoneGetUserInfo($phone)
    {
        return self::where('cellphone', $phone)->find();
    }

    public static function getChild($userid)
    {
        $userid = 1;
        $memberData = self::field('cellphone,userid,up1_userid,up2_userid')->whereOr('up1_userid', $userid)->whereOr('up2_userid', $userid)->select();
        $childData = [];
        foreach ($memberData as $vo) {
            if ($vo['up1_userid'] == $userid) {
                $childData[$vo['userid']]['data'] = $vo;
            } else {
                $childData[$vo['up1_userid']]['child'] = $vo;
            }
        }
        return $childData;
    }

    public static function isPhone($phone)
    {
        $search = '/^1[3|4|5|6|7|8|9][0-9]\d{8}$/';
        if (preg_match($search, $phone)) {
            return (true);
        } else {
            return (false);
        }
    }

    public static function convertedPssword($password)
    {
        return password_hash($password, PASSWORD_BCRYPT);
    }

    public static function verifyPssword($password, $hash)
    {
        return password_verify($password, $hash);
    }

    public static function login($phone, $password)
    {
        if (!static::isPhone($phone)) {
            return ['code' => 4001, 'msg' => lang(4001)];
        }

        $memberInfo = static::where('cellphone', $phone)->find();

        //调试注释不要更新
        if (!$memberInfo || !static::verifyPssword($password, $memberInfo['password'])) {
            return ['code' => 4002, 'msg' => lang(4002)];
        }

        if ($memberInfo['status'] == 1) {
            return ['code' => 4004, 'msg' => lang(4004)];
        }

        $memberInfo->info->save([
            'lastloginip' => Request::ip(),
            'logintimes' => static::raw('logintimes + 1'),
        ]);

        session('memberInfo', clone $memberInfo);
        unset($memberInfo['password']);
        unset($memberInfo['info']);

        return ['code' => 0, 'data' => $memberInfo];
    }

    public static function register($memberInfo)
    {
        if (!static::isPhone($memberInfo['cellphone'])) {
            return ['code' => 4001, 'msg' => lang(4001)];
        }

        $memberInfoRow = static::phoneGetUserInfo($memberInfo['cellphone']);
        if ($memberInfoRow) {
            return ['code' => 4021, 'msg' => lang(4021)];
        }

        $memberInfoRow = static::useridGetUserInfo($memberInfo['up1_userid']);
        if ($memberInfoRow) {
            $memberInfo['up2_userid'] = $memberInfoRow['up1_userid'];
        } else {
            return ['code' => 4023, 'msg' => lang(4023)];
        }
        // if($memberInfo['up1_userid']){
        //     $memberInfoRow = $this->useridGetUserInfo($memberInfo['up1_userid']);
        //     if ($memberInfoRow) {
        //         $memberInfo['up2_userid'] = $memberInfoRow['up1_userid'];
        //     } else {
        //         return ['code' => 4023, 'msg' => lang(4023)];
        //     }
        // }else{
        //     $memberInfo['up1_userid']=0;
        //     $memberInfo['up2_userid']=0;
        // }
        $buyer = new MemberBuyer();
        $affectedRows = $buyer->allowField(['cellphone', 'password', 'up1_userid', 'up2_userid', 'qq'])->save($memberInfo);
        $buyer->info()->save([
            'userid' => $buyer['userid'],
            'regip' => Request::ip(),
            'lastloginip' => Request::ip(),
            'logintimes' => 0,
            'ordertimes' => 0,
        ]);

        $memberInfo = static::login($buyer->cellphone, $memberInfo['password']);

        return $memberInfo;
    }

    function edit_pwd($userid, $password)
    {
        try {
            if (!$this->where('userid', $userid)->update(['password' => $this->convertedPssword($password)])) {
                return ['code' => 1, 'msg' => '修改失败'];
            }
        } catch (ValidateException $e) {
            return ['code' => 1, 'msg' => $e->getError()];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }

        return ['code' => 0];
    }

}